﻿using System;
using System.Data;
using System.Web.Mvc;
using PMSMVC.Models;

namespace PMSMVC.Controllers
{
    [Authorize]
    public class DocTaskCheckController : Controller
    {
        PMSDBContext db = new PMSDBContext();
        private static int taskid;

        //
        // GET: /DocTaskCheck/Create

        public ActionResult Create(int id)
        {
            taskid = id;
            ViewBag.ProjectID = db.DocTasks.Find(taskid).ProjectID;
            ViewBag.TaskName = db.DocTasks.Find(taskid).TaskName;
            return View();
        }

        //
        // POST: /DocTaskCheck/Create

        [HttpPost]
        public ActionResult Create(DocTaskCheck doctaskcheck)
        {
            // TODO: Add insert logic here
            if (ModelState.IsValid)
            {
                doctaskcheck.TaskID = taskid;
                doctaskcheck.CheckTime = DateTime.Now;
                doctaskcheck.UserID = User.Identity.Name;
                //db.DocTaskChecks.Add(doctaskcheck); 
                db.Entry(doctaskcheck).State = EntityState.Added;

                DocTask doctask = db.DocTasks.Find(taskid);
                doctask.TaskStatus = "CLOSE";
                db.Entry(doctask).State = EntityState.Modified;
                db.SaveChanges();

                string projectID = doctask.ProjectID;
                return RedirectToAction("Index", "DocTask", new { projectID = projectID });
            }
            else
            {
                return View(doctaskcheck);
            }
        }
    }
}
